<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\ReplyRequest;
use App\Models\Reply;
use App\Models\Topic;
use App\Models\User;
use App\Transformers\ReplyTransformer;
use Dingo\Api\Auth\Auth;
use Illuminate\Http\Request;

class RepliesController extends Controller
{
    public function store(ReplyRequest $request , Reply $reply,Topic $topic)
    {
        $reply = Reply::create([
            'content' => $request->input('content'),
            'topic_id' => $topic->id,
            'user_id' => $this->user()->id,
        ]);
//        $reply->content = $request->input('content');
//        $reply->topic_id = $topic->id;
//        $reply->user_id = $this->user()->id;
//        $reply->save();

        return $this->response->item($reply,new ReplyTransformer())->setStatusCode(201);
    }

    public function destroy(Topic $topic,Reply $reply)
    {
        if($reply->topic_id != $topic->id){
            return $this->response->errorBadRequest();
        }
        $this->authorize('destroy',$reply);
        $reply->delete();
        return $this->response->noContent();
    }

    public function index(Topic $topic)
    {
        $replies = Reply::query()->where('topic_id',$topic->id)->paginate(20);
        return $this->response->paginator($replies,new ReplyTransformer());
    }

    public function userIndex(User $user)
    {
        $replies = $user->replies()->paginate(20);
        return $this->response->paginator($replies,new ReplyTransformer());
    }
}
